package org.buptsse.ui;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.buptsse.entity.FilterResultItem;
import org.buptsse.entity.User;
import org.buptsse.thread.FindSellByFilterThread;
import org.buptsse.utils.APPConstant;
import org.buptsse.utils.ActionUtils;
import org.buptsse.utils.GloableInstance;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.ParseException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SearchActivity extends Activity implements OnClickListener {

	
	private String[] schoolNo={"我所在学校","北京大学","北京邮电大学","北京师范大学","北京航空航天大学","北京理工大学","北京交通大学","北京科技大学"};
	private String[] priceNo={"不限","20元以下","50元以下","50元以上"};
	private String[] conditionNo={"不限","全新","九成新","七八成新","七成新以下"};
	private EditText sellerSchoolTV,priceTV,conditionTV,keyWordTV;
	private Button searchButton;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 去掉任务条
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.lsearch_one);
		sellerSchoolTV=(EditText) findViewById(R.id.seller_school);
		sellerSchoolTV.setText("我所在学校");
		sellerSchoolTV.setOnClickListener(this);
		priceTV=(EditText) findViewById(R.id.price);
		priceTV.setText("不限");
		priceTV.setOnClickListener(this);
		conditionTV=(EditText) findViewById(R.id.condition);
		conditionTV.setText("不限");
		conditionTV.setOnClickListener(this);
		keyWordTV=(EditText) findViewById(R.id.key_word);
		searchButton=(Button) findViewById(R.id.search_button);
		searchButton.setOnClickListener(this);
	}


	
	private void schoolDialog() {
		new AlertDialog.Builder(this).setTitle("所在学校")
				.setItems(schoolNo, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						sellerSchoolTV.setText(schoolNo[which]);
					}
				}).show();
	}
	
	private void priceDialog() {
		new AlertDialog.Builder(this).setTitle("价格区间")
				.setItems(priceNo, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						priceTV.setText(priceNo[which]);
					}
				}).show();
	}
	
	private void conditionDialog() {
		new AlertDialog.Builder(this).setTitle("新旧程度")
				.setItems(conditionNo, new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						conditionTV.setText(conditionNo[which]);
					}
				}).show();
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.seller_school:
			//卖家所在学校
			schoolDialog();
			break;
		case R.id.price:
			//价格
			priceDialog();
			break;
		case R.id.condition:
			//成色
			conditionDialog();
			break;
		case R.id.search_button:
			//搜索按钮
			String sellerSchool=sellerSchoolTV.getText().toString();
			sellerSchool=sellerSchool.equals("不限")?null:sellerSchool;
			String condition=conditionTV.getText().toString();
			condition=condition.equals("不限")?null:condition;
			String keyWord=keyWordTV.getText().toString();
			keyWord=keyWord.equals("")?null:keyWord;
			String price=priceTV.getText().toString();
			int priceInt=-1;
			if(price.equals("不限"))
			{
				priceInt=-1;
			}else if(price.equals("20元以下"))
			{
				priceInt=20;
			}else if(price.equals("50元以下"))
			{
				priceInt=50;
			}else if(price.equals("50元以上"))
			{
				priceInt=-1;
			}
			Intent intent=new Intent();
			intent.putExtra("sellerschool", sellerSchool);
			intent.putExtra("condition", condition);
			intent.putExtra("keyword", keyWord);
			intent.putExtra("price", priceInt);
			//FindSellByFilterThread fsbft=new FindSellByFilterThread(getApplicationContext(), handler, GloableInstance.user.getName(), sellerSchool, condition, keyWord, priceInt);
			//fsbft.start();
			//ShowWaittingDialog("正在查询");
			intent.setClass(SearchActivity.this, SearchResultActivity.class);
			startActivity(intent);
			finish();
			break;
		}
	}
}